package at.fhj.itm.view;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Screen;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import at.fhj.itm.controller.DictControllerV2;

public class TranslationBoxV2 extends MIDlet
{
	//Commands for main menu
	public Command startC,helpC,aboutUsC,bookmarksC;//,backC_main;
	
	//Commands for search menu
	public Command searchC, translateC, backspace;
	
	//Commands for the entry menu
	public Command bookmarkC; //,backC_entry;	
	public Command exitC,backC;
	
	//Controller
	private DictControllerV2 controller;
	
	private Display display;	
	private Form menuForm,userInputForm,suggestForm,resultForm;
	private TextField dataInput, searchData;
	private Screen mainScr,searchScr, nameScr;
	public static final String SEARCH_SCR_TITLE="Übersetzen";
	public static final int START_INDEX = 0;
	public static final int HELP_INDEX = 1;
	public static final int ABOUT_INDEX = 2;
	private String[] mainScrChoices = {"START","HELP","ABOUT US"};
	private Form entryScr, aboutScr, helpScr;
	public static final String ENTRY_SCR_TITLE="Übersetzung";
	//a ChoiceGroup may be added to a Form in contrast to a List
	private ChoiceGroup searchResults;
	private List vocabSuggestions;
	public static final String VOCAB_SUGGEST_TITLE="Treffer";
	public static final String MAIN_SCR_TITLE="Hauptmenü";
	public static final String ABOUT_SCR_TITLE="Der Begleiter";
	public static final String HELP_SCR_TITLE="Hilfe";
	private String aboutText="";
	private String helpText="";
	
	public TranslationBoxV2()
	{
		display = Display.getDisplay(this);
		controller = new DictControllerV2(this);
		
		
		menuForm      = new Form("DerBegleiter");
		resultForm    = new Form("Uebersetzung");		
		
		startC   = new Command("Select", Command.OK,   1);
		exitC    = new Command("Exit",  Command.EXIT, 1);
		backC    = new Command("Back",  Command.BACK, 1);
		helpC    = new Command("Help",  Command.OK,1);
		aboutUsC = new Command("About Us", Command.OK,1);			
	}

	protected void destroyApp(boolean unconditional)
			throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub
		
	}

	protected void startApp() throws MIDletStateChangeException 
	{
		mainScr = (List)genMainScr();	
		setCurrentScreen(mainScr);
		
	}
	
	public void setCurrentScreen(Displayable d)
	{
		display.setCurrent(d);
	}
	/**
	 * Create the first screen of our MIDLet.
	 * This screen is a list.
	 */
	public Screen genMainScr(){
		
		if(mainScr == null)
		{
			mainScr = new List(MAIN_SCR_TITLE, List.IMPLICIT,mainScrChoices,null);
			exitC    = new Command("Exit",  Command.EXIT, 1);
			mainScr.addCommand(exitC);
			startC   = new Command("Select", Command.OK,   1);
			mainScr.addCommand(startC);
			mainScr.setCommandListener(controller);
//////////////////////just test
//			List test = (List)mainScr;
//			test.append(controller.readFromPackagedFile(), null);
			//////////////////////////////////////////
					
		}		
		return mainScr;
	}	
	
	public Screen genAboutScreen(){
		if(aboutScr==null)
		{
			aboutScr = new Form(ABOUT_SCR_TITLE);
			aboutScr.append(getAboutText());
			aboutScr.addCommand(backC);
			aboutScr.setCommandListener(controller);
		}
		return aboutScr;
	}	
	
	public Screen genHelpScreen()
	{
		if(helpScr==null)
		{
			helpScr = new Form(HELP_SCR_TITLE);
			helpScr.append(getHelpText());
			helpScr.addCommand(backC);
			helpScr.setCommandListener(controller);
		}
		return helpScr;
	}
	
	/**
	 * Display Textfield showing the translation for the vocab selected by the user	 *    
	 */
	public Screen genEntryScr()
	{		
		if(entryScr == null)
		{
			entryScr = new Form("Übersetzung");
			entryScr.addCommand(exitC);
			entryScr.addCommand(backC);
			entryScr.setCommandListener(controller);
//			getData = new TextField("Gib ein Wort ein", null, 30, TextField.PLAIN);
		}		
//		getData.delete(0, getData.size());
		
		return entryScr;
	}
	/**
	 * Search screen
	 * 
	 * Displays one textfield, this used for searching the dictionary 
	 */
	public Screen genSearchScr()
	{		
		if(searchScr == null)
		{			
//			searchResults = new List("Search",List.IMPLICIT);
			userInputForm = new Form(SEARCH_SCR_TITLE);
			
			searchData = new TextField("Gib ein Wort ein", null, 30, TextField.PLAIN);
//			searchData.setItemCommandListener(controller);
			userInputForm.append(searchData);
			searchResults=new ChoiceGroup("Suchergebnisse", ChoiceGroup.EXCLUSIVE);
			userInputForm.append(searchResults);
			userInputForm.addCommand(exitC);
			userInputForm.addCommand(backC);
			translateC = new Command("translate",Command.OK,1);
			searchC = new Command("search", Command.OK,1);
			userInputForm.addCommand(searchC);			
			
			userInputForm.setCommandListener(controller);			
			
			searchScr=userInputForm;			
		}		
				
		return searchScr;		
	}

	public TextField getSearchData() {
		return searchData;
	}

	public void setSearchData(TextField searchData) 
	{
		if(searchData==null)
		{
			throw new IllegalArgumentException();
		}
		this.searchData = searchData;
	}	
	
	public Screen getMainScr() {
		return mainScr;
	}

	public void setMainScr(Screen mainScr) {
		if(mainScr==null)
		{
			throw new IllegalArgumentException("main screen is null!");
		}
		this.mainScr = mainScr;
	}

	public Screen getSearchScr() {
		return searchScr;
	}

	public void setSearchScr(Screen searchScr) {
		if(searchScr==null)
		{
			throw new IllegalArgumentException("search screen is null!");
		}
		this.searchScr = searchScr;
	}

	public Screen getNameScr() {
		return nameScr;
	}

	public void setNameScr(Screen nameScr) {
		if(nameScr==null)
		{
			throw new IllegalArgumentException("name screen is null");
		}
		this.nameScr = nameScr;
	}

	public Form getEntryScr() {		
		return entryScr;
	}

	public void setEntryScr(Form entryScr) {
		if(entryScr==null)
		{
			throw new IllegalArgumentException("entry screen is null");
		}
		this.entryScr = entryScr;
	}	
	

	public ChoiceGroup getSearchResults() {
		return searchResults;
	}

	public void setSearchResults(ChoiceGroup searchResults) {
		if(searchResults==null)
		{
			throw new IllegalArgumentException("Choice group search results is null!");
		}
		this.searchResults = searchResults;
	}

	public List getVocabSuggestions() {
		return vocabSuggestions;
	}

	public void setVocabSuggestions(List vocabSuggestions) {
		if(vocabSuggestions==null)
		{
			throw new IllegalArgumentException("vocab suggestion list is null!");
		}
		this.vocabSuggestions = vocabSuggestions;
	}
	

	public List genVocabSuggestionsList()
	{
		if(vocabSuggestions==null)
		{
			vocabSuggestions = new List(VOCAB_SUGGEST_TITLE,List.IMPLICIT);
			translateC = new Command("translate",Command.OK,1);
			vocabSuggestions.addCommand(translateC);
			vocabSuggestions.addCommand(exitC);
			vocabSuggestions.addCommand(backC);
			vocabSuggestions.setCommandListener(controller);
		}
		return vocabSuggestions;
	}

	public String getAboutText() {
		return aboutText;
	}

	public void setAboutText(String aboutText) {
		if(aboutText==null)
		{
			throw new IllegalArgumentException("about text is null!");
		}
		this.aboutText = aboutText;
	}

	public Form getAboutScr() {
		return aboutScr;
	}

	public void setAboutScr(Form aboutScr) {
		if(aboutScr==null)
		{
			throw new IllegalArgumentException("About screen is null!");
		}
		this.aboutScr = aboutScr;
	}

	public String getHelpText() {
		return helpText;
	}

	public void setHelpText(String helpText) {
		if(helpText==null)
		{
			throw new IllegalArgumentException("help text is null!");
		}
		this.helpText = helpText;
	}

	public Form getHelpScr() {
		return helpScr;
	}

	public void setHelpScr(Form helpScr) {
		if(helpScr==null)
		{
			throw new IllegalArgumentException("help screen is null!");
		}
		this.helpScr = helpScr;
	}
	
	
	
	
	
	
}
